/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package actions

import (
	"gitee.com/tugoer/orivil/mux"
	"strings"
	"gitee.com/tugoer/psy-testing/code/model"
	"strconv"
	"gitee.com/tugoer/psy-testing/code/api/tags"
)

var Result = []*mux.Action {
	{
		Name: "Get Result",
		Method: "GET",
		Route: "/result",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&getResult{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &getResult{}
			ctx.WarnParse(p)
			p.Answer = strings.TrimSpace(p.Answer)
			p.Answer = strings.ToUpper(p.Answer)
			if len(p.Answer) == 1 {
				r := []rune(p.Answer)[0]
				if 'A' <= r && r <= 'Z' {
					res, err := model.GetResultByAnswer(p.SubjectID, p.Answer)
					ctx.ErrWarning(err)
					ctx.SendJson(map[string]string{
						"img": res.Image,
					})
				}
			}
			weight, err := strconv.Atoi(p.Answer)
			ctx.ErrWarning(err)
			res, err := model.GetResultByWeight(p.SubjectID, weight)
			ctx.ErrWarning(err)
			ctx.SendJson(map[string]string{
				"img": res.Image,
			})
		},
		ApiDoc:&mux.ApiDoc {
			Tags:[]string{tags.Results},
			Summary: "查询答题结果",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example: map[string]string{
						"img": "image/result/4581-A.jpg",
					},
				},
			},
		},
	},
	{
		Name: "Get Subject Results",
		Method: "GET",
		Route: "/results",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&getSubjectResults{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &getSubjectResults{}
			ctx.WarnParse(p)
			ress, err := model.GetSubjectResults(p.SubjectID)
			ctx.ErrWarning(err)
			ctx.SendJson(ress)
		},
		ApiDoc:&mux.ApiDoc {
			Tags:[]string{tags.Results},
			Summary: "查询主题的所有答题结果",
			Description: "如果结果不存在则返回空值",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example: []*model.Result{{}},
				},
			},
		},
	},
	{
		Name: "Crate Result",
		Method: "POST",
		Route: "/result",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&model.Result{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Result{}
			ctx.WarnParse(p)
			ctx.ErrWarning(p.Create())
			ctx.SendJson(map[string]int{
				"id": p.ID,
			})
		},
		ApiDoc:&mux.ApiDoc {
			Tags:[]string{tags.Results},
			Summary: "创建答题结果",
			Responses:mux.Responses{
				"200": {
					Description:"success",
					Example: map[string]int{
						"id": 2345,
					},
				},
			},
		},
	},
	{
		Name: "Update Result",
		Method: "PUT",
		Route: "/result",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&model.Result{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Result{}
			ctx.WarnParse(p)
			ctx.ErrWarning(p.Update())
			ctx.MsgSuccess("修改成功", "")
		},
		ApiDoc:&mux.ApiDoc {
			Tags:[]string{tags.Results},
			Summary: "修改答题结果",
		},
	},
	{
		Name: "Delete Result",
		Method: "DELETE",
		Route: "/result",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&model.Result{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Result{}
			ctx.WarnParse(p)
			ctx.ErrWarning(p.Update())
			ctx.MsgSuccess("修改成功", "")
		},
		ApiDoc:&mux.ApiDoc {
			Tags:[]string{tags.Results},
			Summary: "修改答题结果",
		},
	},
}

type getResult struct {
	SubjectID string `desc:"主题ID" required:"主题ID不能为空"`
	Answer string `desc:"最终答案, 最终答案分两种：一种是所有所选答案 weight 相加的结果, 另一种是最后一题的 next 选项" required:"最终答案不能为空"`
}

type getSubjectResults struct {
	SubjectID string `desc:"主题ID" required:"主题 ID 不能为空"`
}
